/*
Copyright 2008, 2009 Joonas Lehtolahti

This file is part of Openfire-C.

Openfire-C is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Openfire-C is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with Openfire-C.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef _XFIREPACKETHELPER_H_
#define _XFIREPACKETHELPER_H_

#include "networking.h"

#define SHORT_ATTRIBUTE(X) (unsigned const char *)#X

/* Attribute creation helper functions */
DLLEXPORT(struct XfirePattr*) XfireAttributeString(non0string *name, non0string *content);
DLLEXPORT(struct XfirePattr*) XfireAttributeInt(non0string *name, unsigned int content);
DLLEXPORT(struct XfirePattr*) XfireAttributeSid(non0string *name, non0string *content);
DLLEXPORT(struct XfirePattr*) XfireAttributeGcid(non0string *name, non0string *content);
DLLEXPORT(struct XfirePattr*) XfireAttributeList(non0string *name, struct XfirePattr4 *content);
DLLEXPORT(struct XfirePattr*) XfireAttributePacket(non0string *name, struct XfirePacket *content);

DLLEXPORT(int) XfirePacketSetAttribute(struct XfirePacket *packet, unsigned int index, struct XfirePattr *attribute);
/* */

/* Data retrieval helper functions */
DLLEXPORT(struct XfirePattr*) XfirePacketGetAttributeByName(struct XfirePacket *packet, non0string *name);
DLLEXPORT(LinkedList*) XfirePacketGetListAttribute(struct XfirePacket *attr, unsigned const char *);
DLLEXPORT(non0string*) XfirePacketGetStringAttribute(struct XfirePacket *attr, unsigned const char *);
DLLEXPORT(struct XfirePacket*) XfirePacketGetPacketAttribute(struct XfirePacket *packet, unsigned const char *name);
DLLEXPORT(unsigned int*) XfirePacketGetIntegerAttribute(struct XfirePacket *attr, unsigned const char *);
/* */

/* Data handling helpers */

struct PairedList {
	LinkedList *names;
	LinkedList *values;
};

DLLEXPORT(struct PairedList*) XfireStringPairsSplit(non0string *pairs);

DLLEXPORT(struct XfirePattr4*) XfirePacketCreateListAttribute(LinkedList *list, unsigned int aid);
/* */

/* Networking helper functions */
DLLEXPORT(int) XfireRecv(int sock, non0string **data, unsigned char *recvbuffer, size_t *current_buffer_length);
DLLEXPORT(int) XfireSend(int sock, non0string  *data, size_t completed);
DLLEXPORT(int) XfSendAll(int s, non0string *data);
DLLEXPORT(int) XfRecvAll(int s, non0string **data, unsigned char *buffer, size_t *buflength);
/* */

/* Packet creation helper functions */
DLLEXPORT(struct XfirePacket*) XfirePHo_Init(unsigned int pid, unsigned int attrs, int shortnames);
DLLEXPORT(int) XfirePHo_AddAttribute(struct XfirePacket *p, const unsigned char *name, unsigned int aid, void *data);
DLLEXPORT(non0string*) XfirePHo_HashWithSalt(non0string *pwhash, non0string *salt);
DLLEXPORT(non0string*) XfirePHo_HashPassword(non0string *username, non0string *password);
DLLEXPORT(non0string*) XfirePHo_HashPasswordWithSalt(non0string *username, non0string *password, non0string *salt);
/* */

#endif
